//
//  ChoiceDateView.m
//  MemberProject
//
//  Created by vincent luo on 15/4/27.
//  Copyright (c) 2015年 MAC OS X. All rights reserved.
//

#import "ChoiceDateView.h"

#define DateViewHeight 250
@implementation ChoiceDateView
{
    NSMutableArray *yearArr;
    NSMutableArray *monthArr;
    NSMutableArray *dayArr;
    NSMutableArray *hourArr;
    NSMutableArray *minArr;
}
-(void)setupView
{
    [self.doneActionBtn addTarget:self action:@selector(doneAction:) forControlEvents:64];
    [self.backActionBtn addTarget:self action:@selector(backAction:) forControlEvents:64];
}
-(void)show:(UIViewController *)controller
{
    CGFloat y = 0;
    UIViewController *onVC = nil;
    if (controller.tabBarController) {
        onVC = controller.tabBarController;
    }else
    {
        onVC = controller;
    }
    y = onVC.view.height;
    
    if (![onVC.view.subviews containsObject:self]) {
        self.frame = CGRectMake(0, y, SCREEN_WIDTH, DateViewHeight);
        [onVC.view addSubview:self];
        
        [UIView beginAnimations:@"choiceDate" context:nil];
        [UIView setAnimationDuration:0.2];
        
        self.frame =CGRectMake(0, y-DateViewHeight, SCREEN_WIDTH, DateViewHeight);
        [UIView commitAnimations];
    }

    
    
//    CGFloat y = controller.tabBarController.view.height;
//    if (![controller.tabBarController.view.subviews containsObject:self]) {
//        self.frame = CGRectMake(0, y, SCREEN_WIDTH, DateViewHeight);
//        [controller.tabBarController.view addSubview:self];
//        
//        [UIView beginAnimations:@"choiceDate" context:nil];
//        [UIView setAnimationDuration:0.2];
//        
//        self.frame =CGRectMake(0, y-DateViewHeight, SCREEN_WIDTH, DateViewHeight);
//        [UIView commitAnimations];
//    }
}
-(void)doneAction:(UIButton *)sender
{
    
    NSString *year = yearArr[[self.pickerView selectedRowInComponent:0]];
    NSString *month = monthArr[[self.pickerView selectedRowInComponent:1]];
    NSString *day = dayArr[[self.pickerView selectedRowInComponent:2]];
    NSString *hour = hourArr[[self.pickerView selectedRowInComponent:3]];
    NSString *min = minArr[[self.pickerView selectedRowInComponent:4]];
    if ([Utils validataDate:[NSString stringWithFormat:@"%@-%@-%@",year,month,day]]) {
        NSString *str = [NSString stringWithFormat:@"%@-%@-%@ %@:%@",year,month,day,hour,min];
        
        self.dBlock(str);
    }else
        [SVProgressHUD showErrorWithStatus:@"时间错误" maskType:SVProgressHUDMaskTypeBlack];
    
    
}
-(void)dealloc
{
    self.dBlock = nil;
    
}
-(void)doneActionBlock:(doneBlock)block
{
    self.dBlock = block;
}

-(void)backAction:(UIButton *)sender
{
    [self backAction];
}
-(void)backAction
{
    [UIView animateWithDuration:0.2 animations:^{
        self.frame = CGRectMake(0, SCREEN_HEIGHT, SCREEN_WIDTH, DateViewHeight);
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
}
-(void)setupData
{
    yearArr = [NSMutableArray new];
    monthArr = [NSMutableArray new];
    dayArr = [NSMutableArray new];
    hourArr = [NSMutableArray new];
    minArr = [NSMutableArray new];
    
    //年
    for (int i=1970; i<2025; i++) {
        [yearArr addObject: [NSString stringWithFormat:@"%d",i]];
    }
    //月
    for (int i=1; i<13; i++) {
        NSString *month = @"";
        if (i<10) {
            month = [NSString stringWithFormat:@"0%d",i];
        }else
            month = [NSString stringWithFormat:@"%d",i];
        
        [monthArr addObject:month];
    }
    //日
    for (int i=1; i<32; i++) {
        NSString *day = @"";
        if (i<10) {
            day = [NSString stringWithFormat:@"0%d",i];
        }else
            day = [NSString stringWithFormat:@"%d",i];
        
        [dayArr addObject:day];
    }
    //小时
    for (int i=0; i<24; i++) {
        NSString *hour = @"";
        if (i<10) {
            hour = [NSString stringWithFormat:@"0%d",i];
        }else
            hour = [NSString stringWithFormat:@"%d",i];
        
        [hourArr addObject:hour];
    }
    //分钟
    for (int i=0; i<60; i++) {
        NSString *minute = @"";
        if (i<10) {
            minute = [NSString stringWithFormat:@"0%d",i];
        }else
            minute = [NSString stringWithFormat:@"%d",i];
        
        [minArr addObject:minute];
    }
}
- (void)drawRect:(CGRect)rect {
    [self setupData];
    [self setupView];
    self.pickerView.dataSource = self;
    self.pickerView.delegate = self;
    
    [self.pickerView selectRow:30 inComponent:0 animated:NO];
    [self.pickerView selectRow:6 inComponent:1 animated:NO];
    [self.pickerView selectRow:0 inComponent:2 animated:NO];
    [self.pickerView selectRow:12 inComponent:3 animated:NO];
    [self.pickerView selectRow:0 inComponent:4 animated:NO];
}
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return 5;
}
- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component
{
    if (component==0) {
        return self.width/5;
    }else
        return self.width/6;
}
- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component
{
    switch (component) {
        case 0:
            return yearArr[row];
            break;
        case 1:
            return monthArr[row];
            break;
        case 2:
            return dayArr[row];
            break;
        case 3:
            return hourArr[row];
            break;
        case 4:
            return minArr[row];
            break;
    }
    return @"";
}

// returns the # of rows in each component..
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    switch (component) {
            case 0:
            return yearArr.count;
            break;
            case 1:
            return monthArr.count;
            break;
            case 2:
            return dayArr.count;
            break;
            case 3:
            return hourArr.count;
            break;
            case 4:
            return minArr.count;
            break;

    }
    return 0;
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.

*/

@end
